//
//  AppDelegate.m
//  CascadeRealm
//
//  Created by QSC on 18/6/24.
//

#import "AppDelegate.h"
#import "CALoginController.h"
#import "CAStartPageController.h"
#import "CABaseNavController.h"
#import "CAMainTabbarController.h"
#import <AppTrackingTransparency/AppTrackingTransparency.h>
@interface AppDelegate ()

@end

@implementation AppDelegate


- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions {
    
    
    self.window = [[UIWindow alloc] initWithFrame:[UIScreen mainScreen].bounds];
    self.window.backgroundColor = UIColor.whiteColor;
    
    [UITableViewCell appearance].selectionStyle = UITableViewCellSelectionStyleNone;
    [UITableViewCell appearance].backgroundColor = Hex(0xFCFFF6);
    [UITableView appearance].backgroundColor =  Hex(0xFCFFF6);
    [UITextField appearance].textColor = Hex(0x333333);
   
    [UITextView appearance].textColor = Hex(0x333333);
    
    BOOL didShowStartPage =  [[NSUserDefaults standardUserDefaults] objectForKey:@"didShowStartPage"];
    
    if (didShowStartPage) {
        
        
        NSString * token = [[NSUserDefaults standardUserDefaults] objectForKey:@"token"];
      
        if(token == nil){
            self.window.rootViewController = [[CABaseNavController alloc]initWithRootViewController:[CALoginController new]];
           
        }else{
            self.window.rootViewController = [UIViewController new];
       
            //验证token
            [Http postWithURL:isValidToken params:@{@"token":token} isShowHud:NO isLogin:NO success:^(id  _Nonnull responseObject) {
                
                [CALoginController loginsuccess:@{}];
               

                
            } failure:^(id _Nonnull error) {
                [[NSUserDefaults standardUserDefaults] removeObjectForKey:@"token"];
                self.window.rootViewController = [[CABaseNavController alloc]initWithRootViewController:[CALoginController new]];
            }];
            
        }
        
    }else{
        
        self.window.rootViewController = [CAStartPageController new];
 
    }
    
    [self.window makeKeyAndVisible];
    
    
    [Http getAppConfig];
    
    [Http PostLogWithPage:@"launch"];
    
    
    return YES;
}


- (void)applicationDidBecomeActive:(UIApplication *)application{
    
    if (@available(iOS 14, *)) {

        [ATTrackingManager requestTrackingAuthorizationWithCompletionHandler:^(ATTrackingManagerAuthorizationStatus status) {

        }];

    }
}


@end
